package com.example.demo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by swallow on 2021/3/24.
 */
@RestController
public class AController {
    @Value("${server.port}")
    private String port;
    @Autowired
    private UserMapper userMapper;

    @GetMapping("login")
    public String login(HttpServletRequest request) {
        String id = request.getSession().getId();
        return "<p>current port " + port + "</p>" +
                "<p>current session " + id + "</p>" +
                "<a href=\"/doLogin?act=u&pwd=p\">doLogin</p>";
    }

    @GetMapping("doLogin")
    public String doLogin(String act, String pwd, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User one = userMapper.selectOne(new QueryWrapper<User>().eq("act", act));
        if (one == null || !one.getPwd().equals(pwd)) return login(request);
        session.setAttribute("name", one.getName());
        return in(request);
    }

    @GetMapping("in")
    public String in(HttpServletRequest request) {
        String id = request.getSession().getId();
        String name = String.valueOf(request.getSession().getAttribute("name"));
        return "<p>current port " + port + "</p>" +
                "<p>current session " + id + "</p>" +
                "<p>current user " + name + "</p>" +
                "<a href=\"/in\">go to [in]</p>";
    }

}
